/**
 * Created by easezhi on 2014/12/23.
 */
var debug = require('debug')('Activity');
var Dao = require('../utils/commonDBConn').Table;

exports.list = function(option, callback){
    debug('执行查询');
    var dao = new Dao('activity');
    var countSql = 'select count(*) total from activity a left join citys c on a.cid=c.cid where true ';
    var dataSql = 'select * from activity a,citys c where a.cid = c.cid ';
    var sql = '';
    var args = [];
    if(option.city){
        sql += ' and c.cid=? ';
        args.push(Number(option.city));
    }
    if(option.toDate){
        sql += ' and createtime<=?';
        args.push(option.toDate);
    }
    if(option.fromDate){
        sql += ' and createtime>=?';
        args.push(option.fromDate);
    }

    if(option.searchName){
        sql += " and title like ?";
        args.push('%'+option.searchName+'%');
    }
    if(!option.sort){
        option.sort = 'createtime';
        option.order = 'desc';
    }
    if(!option.page){
        option.page = 1;
        option.rows = 10;
    }else{
        option.page = Number(option.page);
        option.rows = Number(option.rows);
    }

    dao.executeSql(countSql + sql, args, function(err, count){
        sql += ' order by  '+ option.sort + ' ' + option.order;
        sql += ' limit ?,?';
        args.push( (option.page-1)*option.rows, option.rows);

        if(!err && count[0].total!=0){
            dao.executeSql(dataSql + sql, args, function(err, resultSet){
                if(!err){
                    for(var i=0; i<resultSet.length; i++){
                        resultSet[i].starttime = localeDateTime(resultSet[i].starttime);
                        resultSet[i].endtime = localeDateTime(resultSet[i].endtime);
                        resultSet[i].createtime = localeDateTime(resultSet[i].createtime);
                    }
                    callback({
                        total:count[0].total,
                        rows:resultSet
                    });
                }else{
                    debug(err);
                    callback({
                        total:0,
                        rows:[],
                        err:'err'
                    })
                }
            })
        }else{
            debug('total'+err);
            callback({
                total:0,
                rows:[]
            })
        }
    })

}

function localeDateTime(date){
    if(date instanceof Date){
        return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+' '+date.toLocaleTimeString();
    }else{
        return '';
    }
}

exports.saveUpdate = function(activity, callback){
    var dao = new Dao('activity');
    if(activity.id){
        dao.update('id', activity, function(err, result){
            if(err){
                callback('false');
            }else{
                callback('true');
            }
        })
    }else{
        dao.insert(activity, function(err, result){
            if(err){
                callback('false');
            }else{
                callback('true');
            }
        })
    }
}

exports.delete = function(ids){
    var dao = new Dao('activity');
    dao.delete('id', ids, function(err, result){
        if(err){
            callback('false');
        }else{
            callback('true');
        }
    })
}